﻿@page "/scopes/{id}/mappers/{mapperId}"
@using SimpleIdServer.IdServer.Website.Resources;
@using SimpleIdServer.IdServer.Website.Stores.RealmStore;
@using SimpleIdServer.IdServer.Website.Stores.ScopeStore;
@using SimpleIdServer.IdServer.Website.Stores.UserStore;
@inherits Fluxor.Blazor.Web.Components.FluxorComponent
@layout MainLayout
@inject ContextMenuService contextMenuService
@inject DialogService dialogService
@inject NavigationManager navigationManager
@inject IDispatcher dispatcher
@inject IState<ScopeState> scopeState
@inject NotificationService notificationService

<RadzenBreadCrumb>
    <RadzenBreadCrumbItem Path="/scopes" Text="@Global.Scopes" />
    <RadzenBreadCrumbItem Path=@("/scopes/" + id + "/mappers") Text="@Global.ScopeDetails" />
    <RadzenBreadCrumbItem Text="@Global.MapperDetails" />
</RadzenBreadCrumb>

<RadzenText class="mt-3 mb-3 no-margin" Text="@mapper?.Name" TextStyle="TextStyle.DisplayH3" />

@if (mapper != null)
{
    <div class="mb-1">
        <RadzenBadge Text="@(Enum.GetName(typeof(MappingRuleTypes), mapper.MapperType))" IsPill="true" />
    </div>
}

<RadzenCardLoader class="m-3" Variant=Variant.Filled IsLoading=@scopeState.Value.IsLoading>
    @if(mapper != null)
    {
        switch (mapper.MapperType)
        {
            case MappingRuleTypes.USERATTRIBUTE:
                <UserAttributeScopeMapper ScopeMapperSaved="UpdateScopeMapper" Scope="@scopeState.Value.Scope" ClaimMapper="@mapper" IsEditModeEnabled="true"></UserAttributeScopeMapper>
                break;
            case MappingRuleTypes.USERPROPERTY:
                <UserPropertyScopeMapper ScopeMapperSaved="UpdateScopeMapper" Scope="@scopeState.Value.Scope" ClaimMapper="@mapper" IsEditModeEnabled="true"></UserPropertyScopeMapper>
                break;
            case MappingRuleTypes.SUBJECT:
                <!-- Name -->
                <div>
                    <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.Name</RadzenText>
                    <RadzenTextBox Name="Name" @bind-Value="@mapper.Name" Class="w-100" Disabled="true"></RadzenTextBox>
                </div>
                @if (scopeState.Value.Scope != null)
                {
                    if (scopeState.Value.Scope.Protocol == ScopeProtocols.OPENID)
                    {
                        <!-- Token Claim Name -->
                        <div>
                            <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.TokenClaimName</RadzenText>
                            <RadzenTextBox Name="TokenClaimName" @bind-Value="@mapper.TargetClaimPath" Class="w-100" Disabled=true></RadzenTextBox>
                            <p class="text-muted">@Global.TokenClaimNameDescription</p>
                        </div>
                    }
                    else
                    {
                        <!-- Token Claim Name -->
                        <div>
                            <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.SamlAttributeName</RadzenText>
                            <RadzenTextBox Name="SAMLAttributeName" @bind-Value="@mapper.SAMLAttributeName" Class="w-100"></RadzenTextBox>
                            <p class="text-muted">@Global.SamlAttributeNameDescription</p>
                        </div>
                    }
                }
                break;
        }
    }
</RadzenCardLoader>


@code {
    [Parameter]
    public string id { get; set; } = null!;
    [Parameter]
    public string mapperId { get; set; } = null!;
    ScopeClaimMapper mapper => scopeState.Value.Scope?.ClaimMappers.SingleOrDefault(m => m.Id == mapperId);

    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);
        if (firstRender)
        {
            dispatcher.Dispatch(new GetScopeAction { ScopeId = id });
            SubscribeToAction<UpdateScopeClaimMapperSuccessAction>((act) =>
            {
                notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Success, Summary = Global.ScopeClaimMapperUpdated });
                StateHasChanged();
            });
            SubscribeToAction<SelectRealmSuccessAction>((act) =>
            {
                navigationManager.NavigateTo("/scopes");
            });
        }
    }

    protected override void OnParametersSet()
    {
        base.OnParametersSet();
    }

    void UpdateScopeMapper(ScopeClaimMapper claimMapper)
    {
        claimMapper.Id = mapperId;
        dispatcher.Dispatch(new UpdateScopeClaimMapperAction { ClaimMapper = claimMapper, ScopeId = id });
    }
}